/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef TCPWORK_H
#define TCPWORK_H

#include <QThread>
#include <QObject>
#include <windows.h>
#include "PortBase.h"

namespace SerialPortClass {
const int RX_BUFFER_SIZE = 0x2800;
class TcpPortWork : public QThread {
    Q_OBJECT

public:
    explicit TcpPortWork(QObject *parent = nullptr);
    ~TcpPortWork() override;
    bool InitPort(PortBaseInitStru &tcpPortInit);
    void WriteChar();
    void ClosePort();
    bool IsThreadAlive() const;
    void SetThreadAlive(bool threadAlive);
    void SetOneTimeReadByte(DWORD readByte);

public slots:
    void SlotWriteToPort(const char *string, int n);
signals:
    void SignalReadData(WPARAM wParam, LPARAM lParam);
protected:
    void run() override;
    void ReportBuffer();
private:
    bool CreateSocket(QString ip, UINT port);
    bool Connect(QString ip, UINT port);

    UINT witeThreadMsgId;
    DWORD oneTimeReadByte;
    char *writeBuffer;
    int writeSize;
    char rxBuffer[RX_BUFFER_SIZE];
    UINT alreadyReadByte;
    DWORD writeBufferSize;
    bool isThreadAlive;
    QString ipAddr;
    UINT ipPort;
    SOCKET m_sockClient = INVALID_SOCKET;
    bool isReceive = false;
};
}
#endif // TCPWORK_H
